﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2005
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports DotNetNuke.Web.Mvp

Public Class ViewZParentProjectNameZPresenter
    Inherits ModulePresenter(Of IViewZParentProjectNameZ, ViewZParentProjectNameZModel)

#Region "Constructors"
    Public Sub New(ByVal _View As IViewZParentProjectNameZ)
        Me.New(_View, New ZParentProjectNameZController(New SqlDataProvider()))
    End Sub

    Public Sub New(ByVal _View As IViewZParentProjectNameZ, ByVal _Controller As IZParentProjectNameZController)
        MyBase.New(_View)
        If _Controller Is Nothing Then
            Throw New ArgumentNullException("_Controller", "_Controller is nothing.")
        End If
        Me._Controller = _Controller
    End Sub
#End Region

#Region "Properties and Fields"
    Private _Controller As IZParentProjectNameZController

    Protected ReadOnly Property Controller() As IZParentProjectNameZController
        Get
            Return _Controller
        End Get
    End Property

    Private _Settings As New Dictionary(Of String, String)
    Public Property Settings() As Dictionary(Of String, String)
        Get
            Return _Settings
        End Get
        Set(ByVal value As Dictionary(Of String, String))
            _Settings = value
        End Set
    End Property

    Private ReadOnly Property FormattingTemplate() As String
        Get
            Dim formattedString As String = ""
            If CType(Settings("template"), String) <> "" Then
                formattedString = CType(Settings("template"), String)
            End If
            Return formattedString
        End Get
    End Property
#End Region

#Region "Base Class Overrides"
    Protected Overrides Sub OnInit()
        MyBase.OnInit()
        ' The below is formally from the constructor...
        Try
            View.Model.IsEditable = IsEditable

            Dim colZParentProjectNameZs As List(Of ZParentProjectNameZInfo)

            ' get the content from the ZParentProjectNameZ table
            colZParentProjectNameZs = Controller.GetZParentProjectNameZs(ModuleId)

            If colZParentProjectNameZs.Count = 0 Then
                ' add the content to the ZParentProjectNameZ table
                Dim objZParentProjectNameZ As ZParentProjectNameZInfo = New ZParentProjectNameZInfo
                objZParentProjectNameZ.ModuleId = ModuleId
                objZParentProjectNameZ.Content = "View.Content"
                objZParentProjectNameZ.CreatedByUser = UserId
                objZParentProjectNameZ.ItemId = Controller.AddZParentProjectNameZ(objZParentProjectNameZ)
                colZParentProjectNameZs.Add(objZParentProjectNameZ)
            End If
            For Each obj As ZParentProjectNameZInfo In colZParentProjectNameZs
                If String.IsNullOrEmpty(FormattingTemplate.Trim) Then
                    obj.DisplayContent = obj.Content
                Else
                    obj.DisplayContent = FormatContent(obj)
                End If
            Next
            View.Model.ZParentProjectNameZs = colZParentProjectNameZs
        Catch exc As Exception
            'Show a UI message later
            LogException(exc)
        End Try
    End Sub

    Protected Overrides Sub LoadFromContext()
        MyBase.LoadFromContext()
        If ModuleContext IsNot Nothing Then
            _Settings = New Dictionary(Of String, String)()
            For Each item As DictionaryEntry In ModuleContext.Settings
                _Settings.Add(item.Key.ToString(), item.Value.ToString())
            Next
        End If
    End Sub
#End Region

#Region "Private Methods"
    Private Function FormatContent(ByRef info As ZParentProjectNameZInfo) As String
        Dim FormatString As String = FormattingTemplate
        FormatString = FormatString.Replace("[MODULEID]", info.ModuleId.ToString())
        FormatString = FormatString.Replace("[ITEMID]", info.ItemId.ToString())
        FormatString = FormatString.Replace("[CONTENT]", info.Content)
        FormatString = FormatString.Replace("[CREATEDBYUSER]", info.CreatedByUser.ToString())
        FormatString = FormatString.Replace("[CREATEDBYUSERNAME]", info.CreatedByUserName)
        FormatString = FormatString.Replace("[CREATEDDATE]", info.CreatedDate.ToString())
        Return FormatString
    End Function
#End Region
End Class
